﻿
Ext.namespace('FantastPlatform', 'FantastPlatform.UI');
var placeTypeStore = new Ext.data.Store({
    proxy: new Ext.data.HttpProxy({
        url: '../Service/BusinessService.aspx?Action=GetPlaceType'
    }),
    reader: new Ext.data.JsonReader({
        root: 'topics',
        totalProperty: 'totalCount',
        id: 'Name',
        fields: ['Name', 'Id', 'Code']
    }),
    remoteStore: true
});
placeTypeStore.load();
var purPoseStore = new Ext.data.Store({
    proxy: new Ext.data.HttpProxy({
        url: '../Service/BusinessService.aspx?Action=GetPurPose'
    }),
    reader: new Ext.data.JsonReader({
        root: 'topics',
        totalProperty: 'totalCount',
        id: 'Name',
        fields: ['Name', 'Id', 'Code']
    }),
    remoteStore: true
});
purPoseStore.load();

var responsibilityAreaStore = new Ext.data.Store({
    proxy: new Ext.data.HttpProxy({
        url: '../Service/HouseDistrictService.aspx?Action=GetCommunity'
    }),
    reader: new Ext.data.JsonReader({
        root: 'topics',
        totalProperty: 'totalCount',
        id: 'Name',
        fields: ['Name', 'Id', 'Code']
    }),
    remoteStore: true
});
responsibilityAreaStore.load();
FantastPlatform.UI.BusinessForm = function (config) {
    Ext.apply(this, config);

    var responsibilityAreaStore = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: '../Service/HouseDistrictService.aspx?Action=GetCommunity'
        }),
        reader: new Ext.data.JsonReader({
            root: 'topics',
            totalProperty: 'totalCount',
            id: 'Name',
            fields: ['Name', 'Id', 'Code']
        }),
        remoteStore: true
    });
    Ext.override(Ext.form.CheckboxGroup, {
        //在inputValue中找到定义的内容后，设置到items里的各个checkbox中    
        setValue: function (value) {
            this.items.each(function (f) {
                if (value.indexOf(f.inputValue) != -1) {
                    f.setValue(true);
                } else {
                    f.setValue(false);
                }

            });
        },
        //以value1,value2的形式拼接group内的值   
        getValue: function () {
            var re = "";
            this.items.each(function (f) {
                if (f.getValue() == true) {
                    re += f.inputValue + ",";
                }
            });
            return re.substr(0, re.length - 1);
        },
        //在Field类中定义的getName方法不符合CheckBoxGroup中默认的定义，因此需要重写该方法使其可以被BasicForm找到   
        getName: function () {
            return this.name;
        }
    });

    var group_id;
    this.purPoseStr = 0;
    this.placeTypeStr = 0;
    this.Group_id = group_id;
    var count = placeTypeStore.getCount();
    var placeTypeCheckboxItems = [];
    for (var i = 0; i < count; i++) {
        var boxLabel = placeTypeStore.getAt(i).get("Name");
        var name = placeTypeStore.getAt(i).get("Id");
        var value = placeTypeStore.getAt(i).get("Code");
        placeTypeCheckboxItems.push({
            boxLabel: boxLabel,
            name: 'placeType' + name,
            value: value,
            inputValue: name,
            width: 100
        });
    }
    var count2 = purPoseStore.getCount();
    var pCheckboxItems = [];
    for (var i = 0; i < count2; i++) {
        var boxLabel = purPoseStore.getAt(i).get("Name");
        var name = purPoseStore.getAt(i).get("Id");
        var value = purPoseStore.getAt(i).get("Code");
        pCheckboxItems.push({
            boxLabel: boxLabel,
            name: 'purpose' + name,
            value: value,
            inputValue: name,
            width: 100
        });
    }
    var placeTypeCheckboxGroup = new Ext.form.CheckboxGroup({
        fieldLabel: '<font color="red">*</font>&nbsp;&nbsp;店铺类型',
        name: 'PlaceType',
        hiddenName: 'PlaceType',
        displayField: 'Name',
        mode: 'remote',
        valueField: 'Id',
        allowBlank: false,
        editable: false,
        triggerAction: 'all',
        blankText: '请选择店铺用途',
        columns: 3,
        items: placeTypeCheckboxItems

    });
    var streetComboBox = new FantastPlatform.CaseManagement.SearchField({
        fieldLabel: '<font color="red">*</font>&nbsp;&nbsp;所属街道',
        name: 'StreetName',
        hiddenName: 'StreetId',
        parentField: 'ResponsibilityArea',
        valueField: 'Id',
        url: "../Service/HouseDistrictService.aspx?Action=GetStreet",
        readOnly: true,
        allowBlank: false,
        blankText: '所属街道不能为空',
        width: 130
    });
    var responsibilityAreaComboBox = new FantastPlatform.CaseManagement.SearchField({
        fieldLabel: '<font color="red">*</font>&nbsp;&nbsp;所属责任区',
        name: 'ResponsibilityAreaName',
        hiddenName: 'ResponsibilityArea',
        valueField: 'Id',
        url: "../Service/HouseDistrictService.aspx?Action=GetCommunity",
        readOnly: true,
        allowBlank: false,
        parentField: 'Organization',
        blankText: '选择所属责任区',
        width: 130
    });
    var orgComboBox = new FantastPlatform.CaseManagement.SearchField({
        fieldLabel: '<font color="red">*</font>&nbsp;&nbsp;所属派出所',
        name: 'OrganizationName',
        hiddenName: 'Organization',
        valueField: 'Id',
        url: "../Service/HouseDistrictService.aspx?Action=GetPoliceStation",
        readOnly: true,
        allowBlank: false,
        blankText: '选择所属派出所',
        width: 130
    });
    orgComboBox.on('select', function () {
        var Id = orgComboBox.getValue();
        responsibilityAreaStore.reload({ params: { id: Id} });
    });
    responsibilityAreaComboBox.on('select', function () {
        var Id = responsibilityAreaComboBox.getValue();
        streetStore.reload({ params: { id: Id} });
    });
    var purPoseCheckboxGroup = new Ext.form.CheckboxGroup({
        fieldLabel: '<font color="red">*</font>&nbsp;&nbsp;店铺用途',
        name: 'Purpose',
        hiddenName: 'Purpose',
        displayField: 'Name',
        mode: 'remote',
        valueField: 'Id',
        allowBlank: false,
        blankText: '请选择店铺用途',
        editable: false,
        triggerAction: 'all',
        columns: 3,
        items: pCheckboxItems
    });

    this.items = [{
        layout: 'column',
        border: false,
        labelSeparator: '：',
        items: [
        {
            columnWidth: 1,
            border: false,
            layout: 'form',
            items: [new Ext.form.Hidden({
                name: 'CreatePoliceManNumber'
            })]
        },
        {
            columnWidth: 1,
            border: false,
            layout: 'form',
            items: [new Ext.form.Hidden({
                name: 'Enable'
            })]
        }, {
            columnWidth: 1,
            border: false,
            layout: 'form',
            items: [new Ext.form.Hidden({
                name: 'CreateDate'
            })]
        }, {
            columnWidth: 1,
            border: false,
            layout: 'form',
            items: [new Ext.form.TextField({
                fieldLabel: '<font color="red">*</font>&nbsp;&nbsp;名称',
                name: 'Name',
                allowBlank: false,
                blankText: "名称不能为空",
                anchor: '90%'
            })]
        }, {
            columnWidth: 0.5,
            border: false,
            layout: 'form',
            items: [orgComboBox]
        }, {
            columnWidth: 0.5,
            border: false,
            layout: 'form',
            items: [responsibilityAreaComboBox]
        }, {
            columnWidth: 1,
            border: false,
            layout: 'form',
            items: [streetComboBox]
        }, {
            columnWidth: 1,
            border: false,
            layout: 'form',
            items: [new Ext.form.TextField({
                fieldLabel: '<font color="red">*</font>&nbsp;&nbsp;门牌号',
                name: 'Address',
                allowBlank: false,
                blankText: "门牌号不能为空",
                anchor: '90%'
            })]
        }, {
            columnWidth: 1,
            border: false,
            layout: 'form',
            items: [placeTypeCheckboxGroup]
        }, { columnWidth: 1,
            border: false,
            layout: 'form',
            items: [purPoseCheckboxGroup]
        }, {
            columnWidth: 1,
            border: false,
            layout: 'form',
            items: [new Ext.form.TextField({
                fieldLabel: '经营范围',
                name: 'Range',
                allowBlank: true,             
                anchor: '90%'
            })]
        }, {
            columnWidth: 1,
            border: false,
            layout: 'form',
            items: [new Ext.form.TextField({
                fieldLabel: '备注',
                name: 'Description',
                height: 50,
                anchor: '90%'
            })]
        }]
    }];

    FantastPlatform.UI.BusinessForm.superclass.constructor.call(this, {
        bodyStyle: 'padding:5px',
        collapsible: true,
        labelAlign: 'right',
        autoScroll: true,
        border: false
    });
};

Ext.extend(FantastPlatform.UI.BusinessForm, Ext.form.FormPanel, {
    onRender: function (ct, position) {
        FantastPlatform.UI.BusinessForm.superclass.onRender.call(this, ct, position);
        if (this.Id && this.Id != 0) {
            this.add(new Ext.form.Hidden({ name: 'Id', value: this.Id }));
            var BusinessForm = this.form
            var BusinessId = this.Id
            //responsibilityAreaStore.reload({ callback: function (form, action) { } })
            responsibilityAreaStore.reload({ callback: function (form, action) {
                BusinessForm.load({
                    url: '../Service/BusinessService.aspx?Action=Get',
                    params: { id: BusinessId },
                    waitMsg: '正在读取数据...',
                    waitTitle: '请稍后...',                 
                    failure: function (form, action) {
                        if (action.failureType != 'client') {
                            showErrorMessage('操作失败', action.result.message, '');
                        }
                    }
                });
                Ext.Ajax.request({
                    method: 'post',
                    url: "../Service/BusinessService.aspx?Action=GetPurposeByBusinessId",
                    params: { id: BusinessId },
                    success: function (request, options) {
                        // 服务器返回json形式的结果                                          
                        var reText = request.responseText;
                        var reTextObj = Ext.util.JSON.decode(reText);
                        if (eval(reTextObj.success)) {
                            BusinessForm.findField('Purpose').setValue(reTextObj.message);
                        }
                    }
                });
                Ext.Ajax.request({
                    method: 'post',
                    url: "../Service/BusinessService.aspx?Action=GetPlaceTypeByPersonId",
                    params: { id: BusinessId },
                    success: function (request, options) {
                        // 服务器返回json形式的结果                                          
                        var reText = request.responseText;
                        var reTextObj = Ext.util.JSON.decode(reText);
                        if (eval(reTextObj.success)) {
                            BusinessForm.findField('PlaceType').setValue(reTextObj.message);
                        }
                    }
                });
            }
            });

        }
    }
});
